//-----------------------------------------------------------------------------
// Copyright (c) 2011 David Waby
// Initially created on 01/11/2011
//-----------------------------------------------------------------------------

#include "Global.h"
#include "Character.h"
#include "Map.h"
#include "Tile.h"

//-----------------------------------------------------------------------------
Character::Character()
:	m_map(NULL)
{
} 
		
//-----------------------------------------------------------------------------
Character::~Character()
{
} 

//-----------------------------------------------------------------------------
void Character::Init()
{
	m_characterSheet.InitSetLevel(1);
	m_characterSheet.Init();
}

//-----------------------------------------------------------------------------
bool Character::Move(Direction::Enum const i_direction)
{
	Point2 position = m_position;
	switch (i_direction)
	{
		case Direction::Left:
		{
			--position[X];
			break;
		};
		case Direction::Right:
		{
			++position[X];
			break;
		};
		case Direction::Up:
		{
			++position[Y];
			break;
		};
		case Direction::Down:
		{
			--position[Y];
			break;
		};
		default:
			break;
	};

	if (CanMoveTo(&position))
	{
		MoveTo(&position);
		return true;
	}

	return false;
}

//-----------------------------------------------------------------------------
void Character::Use()
{
	m_map->UseTile(&m_position);
}

//-----------------------------------------------------------------------------
bool Character::CanMoveTo(Point2 const * i_position) const
{
	Tile const * tile = m_map->GetTile(i_position);
	if (tile)
	{
		return tile->HasAttribute(Tile::TileAttributes::Walkable);
	}

	return false;
}

//-----------------------------------------------------------------------------
void Character::MoveTo(Point2 const * i_position)
{
	// Let the previous tile know we've left it
	m_map->ExitTile(i_position);

	// Set the new tile to be where we are
	m_position = *i_position;

	// Let the new tile know we are on it
	m_map->EnterTile(i_position);
}